package com.iwhalecloud.bss.kite.cucc.service.attr;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.ztesoft.bss.common.bo.UserInfo;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.bss.hsf.baseinf.service.InfExchangeService;
import com.iwhalecloud.bss.kite.common.constant.SymbolConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.attr.IUnicomRegionService;
import com.iwhalecloud.bss.kite.cucc.client.api.inf.IInfServiceBus;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.AreaCode;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.AreaInquiry;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.ChannelKindDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.StdAddrQry;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.UnicomRegion;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.UnicomRegionQry;
import com.iwhalecloud.bss.kite.cucc.client.dto.devInfo.response.QueryChannelInfoSerRsp;
import com.iwhalecloud.bss.kite.cucc.client.dto.order.ExecAddressDTO;
import com.iwhalecloud.bss.kite.cucc.common.callclient.HttpsCallClient;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalCallClientEnum;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalErrorConsts;
import com.iwhalecloud.bss.kite.cucc.common.util.CommonUtil;
import com.iwhalecloud.bss.kite.cucc.common.util.KiteAssert;
import com.iwhalecloud.bss.kite.cucc.dataservice.cache.SubRegionCache;
import com.iwhalecloud.bss.kite.cucc.dataservice.cache.UnicomRegionCache;
import com.iwhalecloud.bss.kite.cucc.dataservice.mapper.UnicomRegionMapper;
import com.iwhalecloud.bss.kite.cucc.service.cache.PlatformRegionCache;
import com.iwhalecloud.bss.kite.cucc.service.inf.InfServiceHelper;
import com.iwhalecloud.bss.kite.cucc.service.util.LocalTokenUtil;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.service.context.ServiceContext;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import com.ztesoft.zsmart.core.log.ZSmartLogger;

/**
 * @ClassName : UnicomRegionService
 * @Description : 联通集团区域编码服务
 * @Author : xie.zhengjun
 * @Date : 2020/9/18 16:38
 */
@Service
public class UnicomRegionService implements IUnicomRegionService {

    private static final ZSmartLogger LOGGER = ZSmartLogger.getLogger(UnicomRegionService.class);

    @Value("${interface.call.mode:hsf}")
    private String mode;

    @Resource
    private UnicomRegionMapper unicomRegionMapper;

    @Resource
    private IQuerySceneInstService querySceneInstService;

    @Resource
    private IInfServiceBus infServiceBus;

    @Override
    public List<UnicomRegion> listSubRegion(String regionCode) {
        return KiteStringUtils.isEmpty(regionCode) ?
            addLeafInfo(SubRegionCache.get(SubRegionCache.ROOT_REGION_KEY)) :
            addLeafInfo(SubRegionCache.get(regionCode));
    }

    @Override
    public List<List<UnicomRegion>> listSubRegionMulti(String regionCode) {
        List<List<UnicomRegion>> lists = new ArrayList<>();
        if (KiteStringUtils.isEmpty(regionCode)) {
            List<UnicomRegion> rootUnicomRegions = addLeafInfo(SubRegionCache.get(SubRegionCache.ROOT_REGION_KEY));
            lists.add(rootUnicomRegions);
            return lists;
        }
        String[] regionCodes = KiteStringUtils.split(regionCode, SymbolConsts.COMMA);
        Arrays.stream(regionCodes).forEach(s -> lists.add(listSubRegion(s)));
        return lists;
    }

    @Override
    public UnicomRegion getRegion(String regionCode) {
        if (KiteStringUtils.isEmpty(regionCode)) {
            return null;
        }
        UnicomRegion unicomRegion = UnicomRegionCache.getOneUnicomRegion(regionCode);
        return Optional.ofNullable(unicomRegion).map(region -> {
            region.setIsLeaf(isLeafRegion(region.getRegionCode()));
            return region;
        }).orElse(null);
    }

    @Override
    public boolean isLeafRegion(String regionCode) {
        return KiteListUtils.isEmpty(SubRegionCache.get(regionCode));
    }

    @Override
    public List<ExecAddressDTO> queryStdAddr(StdAddrQry stdAddrQry, String custId) {

        String province;
        String eparchy;
        String city;
        if (KiteStringUtils.isNotEmpty(stdAddrQry.getProdInstId())) {
            province = querySceneInstService.getProdInstAttrValue(custId, stdAddrQry.getProdInstId(), "2_0000220");
            eparchy = querySceneInstService.getProdInstAttrValue(custId, stdAddrQry.getProdInstId(), "2_0000221");
            city = Optional
                .ofNullable(querySceneInstService.getProdInstAttrValue(custId, stdAddrQry.getProdInstId(), "2_0000222"))
                .orElse("");
        }
        else {
            province = stdAddrQry.getProvinceCode();
            eparchy = stdAddrQry.getEparchyCode();
            city = Optional.ofNullable(stdAddrQry.getCityCode()).orElse("");
        }
        KiteAssert.notBlank(province, LocalErrorConsts.PROVINCE_IS_EMPTY);
        KiteAssert.notBlank(eparchy, LocalErrorConsts.EPARCHY_IS_EMPTY);

        Map<String, Object> qryMap = new HashMap<>();
        qryMap.put("PROVINCE_CODE", province);
        qryMap.put("EPARCHY_CODE", eparchy);
        qryMap.put("CITY_CODE", city);
        qryMap.put("INSTALL_ADDRESS", stdAddrQry.getAddrKeyword());
        qryMap.put("PAGE_NUM", stdAddrQry.getPageNum());
        qryMap.put("PAGE_SIZE", stdAddrQry.getPageSize());

        qryMap.put("CHANNEL_ID", "1");
        qryMap.put("CHANNEL_TYPE", "1111111");
        qryMap.put("OPER_ID", ContextUtil.getUserId());
        qryMap.put("QRY_TYPE", "0");

        Map<String, String> routingMap = new HashMap<>();
        routingMap.put("ROUTE_VALUE", "03");
        routingMap.put("ROUTE_TYPE", "00");
        qryMap.put("ROUTING", routingMap);

        return infServiceBus.queryAddress(qryMap);
    }

    @Override
    public List<ChannelKindDTO> queryChannel() {
        return unicomRegionMapper.queryChannel();
    }

    @Override
    public List<ChannelKindDTO> queryChannelByLevel(String channelKindId) {
        return unicomRegionMapper.queryChannelByLevel(channelKindId);
    }

    @Override
    public List<QueryChannelInfoSerRsp> queryChannelByChannelLevel(String chnlKindId) {
        return unicomRegionMapper.queryChannelByChannelLevel(chnlKindId);
    }

    @Override
    public List<UnicomRegion> getUnicomRegions(String regionCode) {
        return SubRegionCache.get(regionCode);
    }



    private List<UnicomRegion> addLeafInfo(List<UnicomRegion> unicomRegions) {
        unicomRegions.forEach(unicomRegion -> unicomRegion.setIsLeaf(isLeafRegion(unicomRegion.getRegionCode())));
        return unicomRegions;
    }

    /**
     * 应用系统区域查询服务(国内)
     * 根据上级区域编码查询省市县子区域信息（调运营平台接口查询）
     * 条件为空，默认查所有省
     * @author zhang.song
     * @date 2021-03-19 11:45
     * @param unicomRegionQry
     * @return java.util.List<com.iwhalecloud.bss.kite.cucc.client.dto.attr.UnicomRegion>
     */
    @Override
    public List<UnicomRegion> innerSubRegionFromPlatform(UnicomRegionQry unicomRegionQry) {
        String locationType = unicomRegionQry.getLocationType();
        locationType = KiteStringUtils.isBlank(locationType) ? "10" : locationType;
        String parentAreaCode = unicomRegionQry.getParentAreaCode();
        if (!KiteStringUtils.equalsAny(locationType, "10", "20", "30")) {
            return new ArrayList<>();
        }
        return PlatformRegionCache.get(locationType, parentAreaCode);
    }

    /**
     * 应用系统区域查询服务(国内+国际)
     * 根据上级区域编码查询省市县子区域信息（调运营平台接口查询）
     * 条件为空，默认查所有省
     * @author zhang.song
     * @date 2021-03-19 11:45
     * @param unicomRegionQry
     * @return java.util.List<com.iwhalecloud.bss.kite.cucc.client.dto.attr.UnicomRegion>
     */
    @Override
    public List<UnicomRegion> subRegionFromPlatform(UnicomRegionQry unicomRegionQry) {
        List<UnicomRegion> result = this.innerSubRegionFromPlatform(unicomRegionQry);

        if(KiteStringUtils.isNotEmpty(unicomRegionQry.getParentAreaCode()) && KiteListUtils.isNotEmpty(result)) {
            return result;
        }

        // 查看配置是否支持国际区域
        String productId = unicomRegionQry.getProductId();
        if(KiteStringUtils.isNotEmpty(productId)) {
            String offerId = null;
            if(KiteStringUtils.isNotEmpty(unicomRegionQry.getSceneInstId())) {
                KiteSceneInst kiteSceneInst = querySceneInstService.getSceneInst(unicomRegionQry.getSceneInstId());
                offerId = Optional.ofNullable(querySceneInstService.getOfferInst(kiteSceneInst.getCustId(), kiteSceneInst.getRootInstId())).map(KiteOfferInst::getOfferId).orElse(null);
            }
            String _offerId = offerId;

            List<DcPublic> dcPublics = DcPublicCache.getByPcode("2021042301", "fieldNameRegion", productId);
            if(KiteListUtils.isNotEmpty(dcPublics)) {
                List<UnicomRegion> interRegion =  this.interSubRegionFromPlatform(unicomRegionQry);
                if(null != unicomRegionQry.getFieldName()) {

                    String postProvinceNbr = Optional.ofNullable(ContextUtil.getLoginInfo().getUserInfo()).map(UserInfo::getPostProvinceNbr).orElse(null);
                    dcPublics.sort((t1, t2) -> {
                        if(KiteStringUtils.isEmpty(t1.getCoded()) && KiteStringUtils.isNotEmpty(t2.getCoded())) {
                            return 1;
                        }
                        return -1;
                    });

                    String fieldRegion = dcPublics.stream().filter(dcPublic ->
                        dcPublic.getCodeb().equals(unicomRegionQry.getFieldName())
                            && (KiteStringUtils.isEmpty(dcPublic.getCoded()) || KiteStringUtils.equalsAny(postProvinceNbr, dcPublic.getCoded().split(SymbolConsts.COMMA)))
                            && (KiteStringUtils.isEmpty(dcPublic.getCodee()) || KiteStringUtils.equalsAny(_offerId, dcPublic.getCodee().split(SymbolConsts.COMMA)))
                    ).findFirst().map(DcPublic::getCodec).orElse(null);

                    if(KiteStringUtils.isEqual(fieldRegion, "in")) {
                        return result;
                    }
                    if(KiteStringUtils.isEqual(fieldRegion, "out")) {
                        return interRegion;
                    }
                    if(KiteStringUtils.isEqual(fieldRegion, "inAndOut")) {
                        result.addAll(interRegion);
                    }
                } else {
                    result.addAll(interRegion);
                }
            }
        }
        return result;
    }

    /**
     * 应用系统区域查询服务(国内+国际)
     * 根据上级区域编码查询省市县子区域信息（调运营平台接口查询）（多个区域编码）
     * 条件为空，默认查所有省
     * @author zhang.song
     * @date 2021-03-20 15:27
     * @param unicomRegionQryList
     * @return java.util.List<java.util.List<com.iwhalecloud.bss.kite.cucc.client.dto.attr.UnicomRegion>>
     */
    @Override
    public List<List<UnicomRegion>> subRegionMultiFromPlatform(List<UnicomRegionQry> unicomRegionQryList) {
//        String nodeCode = querySceneInstService.g(data.getSceneInstId(), "nodeCode");
        List<List<UnicomRegion>> lists = new ArrayList<>();
        if (KiteListUtils.isEmpty(unicomRegionQryList)) {
            List<UnicomRegion> rootUnicomRegions = this.subRegionFromPlatform(new UnicomRegionQry());
            lists.add(rootUnicomRegions);
            return lists;
        } else if (unicomRegionQryList.size() == 1) {
            UnicomRegionQry unicomRegionQry = unicomRegionQryList.get(0);
            String nodeCode = querySceneInstService.getExtParameterValue( unicomRegionQry.getSceneInstId(), "nodeCode");

            if(KiteStringUtils.isEmpty(unicomRegionQry.getParentAreaCode())) {
                List<UnicomRegion> rootUnicomRegions = this.subRegionFromPlatform(unicomRegionQry);
                if("BatchInput".equals(nodeCode)){
                    rootUnicomRegions =rootUnicomRegions.stream().filter(u -> u.getRegionCode().equals(ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr())).collect(Collectors.toList());
                }
                lists.add(rootUnicomRegions);

                return lists;
            }
        }

        boolean isInter = false;
        String productId = unicomRegionQryList.get(0).getProductId();
        if(KiteStringUtils.isNotEmpty(productId)) {
            // 查看配置是否支持国际区域
            List<DcPublic> dcPublics = DcPublicCache.getByPcode("2021042301", "fieldNameRegion", productId);
            if(KiteListUtils.isNotEmpty(dcPublics)) {
                // 根据一级编码，判断当前是国内区域，还是国际区域
                String topAreaCode = unicomRegionQryList.get(0).getTopAreaCode();
                if(KiteStringUtils.isNotEmpty(topAreaCode)) {
                    List<UnicomRegion> innerList = this.interSubRegionFromPlatform(new UnicomRegionQry());
                    for (UnicomRegion unicomRegion : innerList) {
                        if(topAreaCode.equals(unicomRegion.getRegionCode())) {
                            isInter = true;
                            break;
                        }
                    }
                }
            }
        }

        List<UnicomRegion> unicomRegions;
        for (UnicomRegionQry unicomRegionQry : unicomRegionQryList) {
            if(isInter) {
                unicomRegions = this.interSubRegionFromPlatform(unicomRegionQry);
            } else {
                unicomRegions = this.innerSubRegionFromPlatform(unicomRegionQry);
            }
            lists.add(unicomRegions);
        }
        return lists;
    }

    /**
     * 根据上级区域编码查询省市县子区域信息（调运营平台接口查询）
     * @author zhang.song
     * @date 2021-03-20 16:31
     * @param regionCode 依次省市县编码英文逗号隔开
     * @return java.util.List<java.util.List<com.iwhalecloud.bss.kite.cucc.client.dto.attr.UnicomRegion>>
     */
    @Override
    public List<List<UnicomRegion>> subRegionMultiFromPlatformByRegionCode(String regionCode) {
        List<List<UnicomRegion>> lists = new ArrayList<>();
        String[] regionCodes = KiteStringUtils.split(regionCode, SymbolConsts.COMMA);
        if (KiteStringUtils.isEmpty(regionCode)||regionCodes.length<1) {
            List<UnicomRegion> rootUnicomRegions = this.subRegionFromPlatform(new UnicomRegionQry());
            lists.add(rootUnicomRegions);
            return lists;
        }
        boolean isInter = false;
        // 根据一级编码，判断当前是国内区域，还是国际区域
        String topAreaCode = regionCodes[0];
        if(KiteStringUtils.isNotEmpty(topAreaCode)) {
            List<UnicomRegion> innerList = this.interSubRegionFromPlatform(new UnicomRegionQry());
            if("-1".equals(topAreaCode)){
                lists.add(innerList);
                return lists;
            }
            for (UnicomRegion unicomRegion : innerList) {
                if(topAreaCode.equals(unicomRegion.getRegionCode())) {
                    isInter = true;
                    break;
                }
            }
        }

        UnicomRegionQry unicomRegionQry;
        List<UnicomRegion> unicomRegions;
        for (int i = 0; i < regionCodes.length && i < 2; i++) {
            unicomRegionQry = new UnicomRegionQry();
            unicomRegionQry.setParentAreaCode(regionCodes[i]);
            unicomRegionQry.setLocationType(String.valueOf((i + 2) * 10));
            if(isInter) {
                unicomRegions = this.interSubRegionFromPlatform(unicomRegionQry);
            } else {
                unicomRegions = this.innerSubRegionFromPlatform(unicomRegionQry);
            }
            lists.add(unicomRegions);
        }
        return lists;
    }

    /**
     * 应用系统区域查询服务(国际)
     * 根据上级区域编码查询省市县子区域信息（调运营平台接口查询）
     * 条件为空，默认查所有省
     * @author zhang.song
     * @date 2021-04-22 17:30
     * @param unicomRegionQry
     * @return java.util.List<com.iwhalecloud.bss.kite.cucc.client.dto.attr.UnicomRegion>
     */
    @Override
    public List<UnicomRegion> interSubRegionFromPlatform(UnicomRegionQry unicomRegionQry) {
        List<UnicomRegion> result = new ArrayList<>();

        // 批量受理暂时没有国际业务，先返回空集合
        if (ServiceContext.getIsBatchAccept()) {
            return result;
        }

        String locationType = unicomRegionQry.getLocationType();
        if(KiteStringUtils.isNotBlank(locationType) && !KiteStringUtils.equalsAny(locationType, "10", "20", "30")) {
            return result;
        }

        Map<String, Object> requestMap = new HashMap<>();
        Map uniBssBody = new HashMap();
        Map domesticPoliticalLocation = new HashMap();
        Map tcpCont = new HashMap();
        // 系统来源 H：销售
        tcpCont.put("APP_KEY", "H");
        domesticPoliticalLocation.put("TCP_CONT", tcpCont);
        domesticPoliticalLocation.put("LOCATION_TYPE", KiteStringUtils.isBlank(unicomRegionQry.getLocationType())?"10":unicomRegionQry.getLocationType());
        if(KiteStringUtils.isNotBlank(unicomRegionQry.getParentAreaCode())) {
            domesticPoliticalLocation.put("PARENT_AREA_CODE", unicomRegionQry.getParentAreaCode());
        }
        uniBssBody.put("INTER_QUERY_REGION_REQ", domesticPoliticalLocation);
        requestMap.put("UNI_BSS_BODY", uniBssBody);
        requestMap.put("OP_CODE", "INTER_QUERY_REGION");
        InfServiceHelper.adaptGray(requestMap, domesticPoliticalLocation);
        String respStr;
        if (KiteStringUtils.isEqual(mode, "http")) {
            respStr = HttpsCallClient.postStringWithObject(LocalCallClientEnum.INF_COMMON_URL, requestMap, LocalTokenUtil.getHeaderMap());
        } else {
            InfExchangeService infExchangeService = SpringUtil.getBean("infExchangeService");
            respStr = infExchangeService.exchange(new JSONObject(requestMap));
        }

        Map<String, Object> respMap = JSON.parseObject(respStr, Map.class);
        Map headMap = MapUtils.getMap(respMap, "UNI_BSS_HEAD");
        if(!"00000".equals(MapUtils.getString(headMap, "RESP_CODE"))) {
            LOGGER.warn("应用系统区域查询服务(国际)接口失败：{}", MapUtils.getString(headMap, "RESP_DESC"));
            return result;
        }

        Map respBody = MapUtils.getMap(respMap, "UNI_BSS_BODY");
        Map queryRegionRsp = MapUtils.getMap(respBody, "INTER_QUERY_REGION_RSP");
        if(!"00000".equals(MapUtils.getString(queryRegionRsp, "CODE"))) {
            LOGGER.warn("应用系统区域查询服务(国际)接口失败：{}", MapUtils.getString(queryRegionRsp, "MESSAGE"));
            return result;
        }
        List<Map<String, Object>> list = CommonUtil.getList(queryRegionRsp, "DATA");
        if(KiteListUtils.isNotEmpty(list)) {
            UnicomRegion unicomRegion;
            for (Map<String, Object> map : list) {
                unicomRegion = new UnicomRegion();
                unicomRegion.setRegionId(MapUtils.getLong(map, "ID"));
                unicomRegion.setRegionCode(MapUtils.getString(map, "AREA_CODE"));
                unicomRegion.setRegionName(MapUtils.getString(map, "AREA_NAME"));
                unicomRegion.setParRegionCode(MapUtils.getString(map, "PARENT_AREA_CODE"));
                unicomRegion.setRegionLevel(MapUtils.getInteger(map, "LOCATION_TYPE"));
                unicomRegion.setStatusCd(KiteStringUtils.equals("0", MapUtils.getString(map, "STATUS"))?"1000":"1100");
                unicomRegion.setRemark(MapUtils.getString(map, "REMARK"));
                unicomRegion.setIsLeaf(KiteStringUtils.equals("30", MapUtils.getString(map, "LOCATION_TYPE")));
                result.add(unicomRegion);
            }
        }
        return result;
    }

    /**
     * 3.4.行政区域编码转换服务
     * @author zhang.song
     * @date 2021-04-12 11:50
     * @param areaInquiry
     * @return com.iwhalecloud.bss.kite.cucc.client.dto.attr.AreaCode
     */
    @Override
    public AreaCode regionTransform(AreaInquiry areaInquiry) {
        Map<String, Object> requestMap = new HashMap<>();
        Map uniBssBody = new HashMap();
        uniBssBody.put("MIDDLLEPLATFORM_REQ", areaInquiry);
        requestMap.put("UNI_BSS_BODY", uniBssBody);
        requestMap.put("OP_CODE", "json_chinaUnicom_networkMidPlatform_abilityGateway_middlleplatform");
        InfServiceHelper.adaptGray(requestMap, requestMap);
        String respStr =null;

        try {
            if (KiteStringUtils.isEqual(mode, "http")) {
                respStr = HttpsCallClient.postStringWithObject(LocalCallClientEnum.INF_COMMON_URL, requestMap, LocalTokenUtil.getHeaderMap());
            } else {
                InfExchangeService infExchangeService = SpringUtil.getBean("infExchangeService");
                respStr = infExchangeService.exchange(new JSONObject(requestMap));
            }
        } catch (Exception e) {
            ErrorConsts.BUSI_COMMON_ERROR.throwOut("装机地址行政区域转换接口异常，请核实接口服务是否可用[json_chinaUnicom_networkMidPlatform_abilityGateway_middlleplatform]");
        }

        AreaCode areaCode = new AreaCode();
        Map<String, Object> respMap = JSON.parseObject(respStr, Map.class);
        Map headMap = MapUtils.getMap(respMap, "UNI_BSS_HEAD");
        if(!"00000".equals(MapUtils.getString(headMap, "RESP_CODE"))) {
            areaCode.setCode(MapUtils.getString(headMap, "RESP_CODE"));
            areaCode.setMessage(MapUtils.getString(headMap, "RESP_DESC"));
            return areaCode;
        }

        Map respBody = MapUtils.getMap(respMap, "UNI_BSS_BODY");
        Map middllePlatFormRsq = MapUtils.getMap(respBody, "BUILDING_INQUIRY_RSP");
        headMap = MapUtils.getMap(middllePlatFormRsq, "UNI_NET_HEAD");
        if(!"0000".equals(MapUtils.getString(headMap, "RESP_CODE"))) {
            areaCode.setCode(MapUtils.getString(headMap, "RESP_CODE"));
            areaCode.setMessage(MapUtils.getString(headMap, "RESP_DESC"));
            return areaCode;
        }

        Map dataBody = CommonUtil.getMap(middllePlatFormRsq, "UNI_NET_BODY");
        if(!"200".equals(MapUtils.getString(dataBody, "code"))) {
            areaCode.setCode(MapUtils.getString(dataBody, "code"));
            areaCode.setMessage(MapUtils.getString(dataBody, "message"));
            return areaCode;
        }

        Map dataMap = MapUtils.getMap(dataBody, "data");
        areaCode.setCode("00000");
        areaCode.setProvinceCode(MapUtils.getString(dataMap, "provinceCode"));
        areaCode.setCityCode(MapUtils.getString(dataMap, "cityCode"));
        areaCode.setDistrictCode(MapUtils.getString(dataMap, "districtCode"));
        return areaCode;
    }

    /**
     * 根据区域编码列表获取区域详情列表
     * @author zhang.song
     * @date 2021-04-20 20:41
     * @param regionCodeList
     * @return java.util.List<com.iwhalecloud.bss.kite.cucc.client.dto.attr.UnicomRegion>
     */
    @Override
    public List<UnicomRegion> getRegionList(List<String> regionCodeList) {
        if (KiteListUtils.isEmpty(regionCodeList)) {
            return null;
        }

        List<UnicomRegion> unicomRegionList = new ArrayList<>();
        for (String regionCode : regionCodeList) {
            UnicomRegion unicomRegion = UnicomRegionCache.getOneUnicomRegion(regionCode);
            if (!KiteObjectUtils.isEmpty(unicomRegion)) {
                unicomRegionList.add(unicomRegion);
            }
        }
        return unicomRegionList;
    }

    @Override
    public void insertInfAreaInfoBatch(List<UnicomRegion> batchQryAreaInfos) {
        unicomRegionMapper.insertBatch(batchQryAreaInfos);
    }

    @Override
    public void deleteInfAreaInfoBatch(String regionLevel) {
        unicomRegionMapper.deleteInfAreaInfo(regionLevel);
    }
}
